/*
 * Copyright (C) 2009 Jakub Miara
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.araim.rringtone;

import com.araim.rringtone.db.RandomRingtoneDatabase;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * 
 * @author Jakub Miara
 * @version 1.0
 * 
 *          Main Activity of Random Ringtone Application - basically it's a
 *          preferences screen. Allows for turning on/off and moving to ringtone
 *          selection
 * 
 */
public class RandomRingtone extends Activity {

	/**
	 * shared preferences key - random ringtone is on/off
	 */
	public static final String RRINGTONE_ACTIVE = "RRINGTONE_ACTIVE";
	/**
	 * Shared preferences name
	 */
	public static final String RRINGTONE_PREFS_NAME = "araim.rringtone.preferences";
	/**
	 * log tag
	 */
	public static final String TAG = "RingtoneChange";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViewById(R.id.button).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Log.d(TAG, "Starting ringtone selection");
				// create new intent to display ringtone selection activity
				startActivityForResult(new Intent(getApplicationContext(),
						SelectRingtonesActivity.class), 1);
			}
		});

		Uri currentUri = RingtoneManager.getActualDefaultRingtoneUri(
				getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
		Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
				currentUri);
		String ringtone = "None";
		if(r != null){ringtone = r.getTitle(getApplicationContext());}
		TextView tv = ((TextView) findViewById(R.id.CurrentRingtone));
				tv.setText(String.format("Current ringtone: %s",
						ringtone));
		ToggleButton tb = (ToggleButton) findViewById(R.id.onoff);
		SharedPreferences settings = RandomRingtone.this.getSharedPreferences(
				RRINGTONE_PREFS_NAME, 0);

		tb.setChecked(settings.getBoolean(RRINGTONE_ACTIVE, false));
		tb.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					ToggleButton tb = (ToggleButton) v;

					Log.d(TAG, "Random ringtone turned "
							+ (tb.isChecked() ? "on" : "off"));
					// whether the ringtone changing is on or off is stored in
					// shared preferences
					SharedPreferences settings = RandomRingtone.this
							.getSharedPreferences(RRINGTONE_PREFS_NAME, 0);

					SharedPreferences.Editor edtr = settings.edit();
					edtr.putBoolean(RRINGTONE_ACTIVE, tb.isChecked());
					edtr.commit();
				} catch (Exception e) {
					Log.e(TAG, "Error turning the Random Ringtone on/off"
							+ e.toString());
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		try {
			// we only start one activity, so this is the return from ringtone
			// selection
			// if user pressed SAVE (result is OK)
			if (data != null && resultCode == RESULT_OK) {
				// get the list of the ringtones
				Parcelable[] rings = data.getParcelableArrayExtra(SelectRingtonesActivity.SAVED_RINGTONES_LIST);
				RandomRingtoneDatabase rrd = new RandomRingtoneDatabase(getApplicationContext());
				// if any was selected
				if (rings != null) {
					Log.i(TAG, "User selected " + rings.length + " ringtones");
					// clear the database
					rrd.clear();
					// insert each ringtone
					for (Parcelable p : rings) {
						RingtoneInfo ri = (RingtoneInfo) p;
						rrd.insertRingtone(ri);
					}
					// none was selected
				} else {
					rrd.clear();
				}
			}
		} catch (Exception e) {
			Log.e(TAG, "Error on returning from ringtone selection requestcode: " + requestCode + " resultcode: " + resultCode);
			Log.e(TAG, "Error on returning from ringtone selection:" + e.toString());
		}
	}
}